# coding: utf-8

from __future__ import print_function

import can

def send_one():

    # see https://python-can.readthedocs.io/en/stable/configuration.html
    
    # bus = can.interface.Bus()

    bus0 = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=500000)

    msg = can.Message(arbitration_id=0xc0ffee,
                      data=[0, 25, 0, 1, 3, 1, 4, 1],
                      is_extended_id=True)

    try:
        bus0.send(msg)
        print("Message sent on {}".format(bus0.channel_info))
    except can.CanError:
        print("Message NOT sent")

def recv():
    bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=500000)
    
    
    try:
        msg = bus.recv(timeout=5)  #接收信息
        if msg != None:
            print('Message received on {}'.format(bus.channel_info))
            print(msg.data)
    except can.CanError:
        print('Message receiving timeout')



if __name__ == '__main__':
    while True:
        send_one()
        recv()
        
    


